package com.vasthu.core.service;

import com.vasthu.core.dao.RoleDAOImpl;
import com.vasthu.api.dao.RoleDAO;
import com.vasthu.api.domain.Role;
import com.vasthu.api.service.RoleManagerService;
import com.vasthu.gui.VstTableItemSelectable;
import com.vasthu.utility.VstDBManager;
import java.util.*;
import java.sql.*;

public class RoleManagerServiceImpl implements RoleManagerService {

	private RoleDAO roleDao;

	public RoleManagerServiceImpl() {
		roleDao = new RoleDAOImpl();
	}
	
	public boolean insertRole(Role role) {
		if (role == null)
			return false;
			
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = roleDao.insertRole(conn, role);
		} catch (Exception ex) {
			System.out.println("ERROR : RoleManagerServiceImpl->insertRole()");
		}
		
		VstDBManager.get().close();
		return ok;
	}
	
	public Role getRoleById(long roleId) {
		Connection conn = VstDBManager.get().getConnection();
		Role role = null;
		try {
			role = roleDao.getRoleById(conn, roleId);
		} catch (Exception ex) {
			System.out.println("ERROR : RoleManagerServiceImpl->deleteRoleById()");
		}
		
		VstDBManager.get().close();
		return role;
	}
	
	public Role getRoleByName(String roleName) {
		Connection conn = VstDBManager.get().getConnection();
		Role role = null;
		try {
			role = roleDao.getRoleByName(conn, roleName);
		} catch (Exception ex) {
			System.out.println("ERROR : RoleManagerServiceImpl->deleteRoleByName()");
		}
		
		VstDBManager.get().close();
		return role;
	}
	
	public boolean deleteRoleById(long roleId) {
		Connection conn = VstDBManager.get().getConnection();
		boolean ok = false;
		try {
			ok = roleDao.deleteRoleById(conn, roleId);
		} catch (Exception ex) {
			System.out.println("ERROR : RoleManagerServiceImpl->deleteRoleById()");
		}
		
		VstDBManager.get().close();
		return ok;
	}
	
	public List<String> getAllRoleNames() {
		List<Role> roleList = new ArrayList<Role>();
		List<String> roleNameList = new ArrayList<String>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			roleList = roleDao.getAllRoles(conn);
			for (Role role : roleList) {
				roleNameList.add(role.getRoleName());
			}
		} catch (Exception ex) {
			System.out.println("ERROR : RoleManagerServiceImpl->getAllRoleNames()");
		}
		
		VstDBManager.get().close();
		
		return roleNameList;
	}

	public List<VstTableItemSelectable> getAllRolesForTable() {
		List<Role> roleList = new ArrayList<Role>();
		List<VstTableItemSelectable> dataList = new ArrayList<VstTableItemSelectable>();
		
		Connection conn = VstDBManager.get().getConnection();
		
		try {
			roleList = roleDao.getAllRoles(conn);
			for (Role role : roleList) {
				dataList.add(role);
			}
		} catch (Exception ex) {
			System.out.println("ERROR : RoleManagerServiceImpl->getAllRolesForTable()");
		}
		
		VstDBManager.get().close();
		
		return dataList;
	}

}